package com.example.module_device

import android.content.Intent
import android.util.Log
import android.view.Menu
import android.view.MenuItem
import android.view.View
import android.widget.PopupMenu
import androidx.activity.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import cn.jiguang.bv.v
import com.alibaba.android.arouter.facade.annotation.Route
import com.example.lib_common.base.BaseActivity
import com.example.lib_common.base.state.Empty
import com.example.lib_common.base.state.Failed
import com.example.lib_common.base.state.Loading
import com.example.lib_common.base.state.Success
import com.example.lib_common.observeWithLifecycle
import com.example.lib_common.utils.RouterPath
import com.example.lib_common.utils.ToastUtils
import com.example.module_device.adapter.DeviceAdapter
import com.example.module_device.databinding.ActivityDeviceBinding
import com.example.module_device.ui.AddDeviceActivity
import dagger.hilt.android.AndroidEntryPoint


@Route(path = RouterPath.DEVICE_UI)
@AndroidEntryPoint
class DeviceActivity : BaseActivity<DeviceViewModel,ActivityDeviceBinding>() {

    override val mLayoutResId: Int = R.layout.activity_device
    private val mViewModel : DeviceViewModel by viewModels()
    private val mAdapter : DeviceAdapter by lazy { DeviceAdapter() }

    override fun initView() {
        super.initView()
        mViewModel.getDeviceAll()
        initialize()
    }

    override fun initData() {
        super.initData()
        mViewModel.mCarState.observeWithLifecycle(this){
            when(it){
                is Success -> {
                    mViewModel.mDeviceList.observe(this){
                        mAdapter.setList(it)
                    }
                }
                is Error -> {
                    ToastUtils.showShort(this,it.message)
                }
                Empty -> {}
                is Failed -> {}
                Loading -> {}
            }
        }
        mBinding.more.setOnClickListener {
            // 创建 PopupMenu
            val popupMenu = PopupMenu(this, it)  // 通过点击的视图来创建 PopupMenu

            // 加载菜单项
            menuInflater.inflate(R.menu.menu, popupMenu.menu)

            // 处理菜单项的点击事件
            popupMenu.setOnMenuItemClickListener { item: MenuItem ->
                when (item.itemId) {
                    R.id.add_device -> {
                        // 执行选项1的操作
                        startActivity(Intent(this, AddDeviceActivity::class.java))
                        true
                    }
                    else -> false
                }
            }

            // 显示 PopupMenu
            popupMenu.show()
        }
    }

    private fun initialize(){
        mBinding.deviceRv.layoutManager = LinearLayoutManager(this)
        mBinding.deviceRv.adapter = mAdapter

    }

    override fun onResume() {
        super.onResume()
        mViewModel.mCarState.observeWithLifecycle(this){
            when(it){
                is Success -> {
                    mViewModel.mDeviceList.observe(this){
                        mAdapter.setList(it)
                    }
                }
                is Error -> {
                    ToastUtils.showShort(this,it.message)
                }
                Empty -> {}
                is Failed -> {}
                Loading -> {}
            }
        }
    }

}